package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;

    /**
     * 根据分类id查询所有的规格参数组信息
     * @param cid 分类id
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupsByCid(@RequestParam("id")Long cid) {
        return ResponseEntity.ok(specGroupService.querySpecGroupsByCid(cid));
    }


    /**
     * 根据分类id查询规格组及组内参数
     * @param cid 分类id
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupAndParamByCid(@RequestParam("id")Long cid) {
        return ResponseEntity.ok(specGroupService.queryGroupAndParamByCid(cid));
    }


    /**
     * 新增规格组
     */
    @PostMapping("/group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        specGroupService.addSpecGroup(specGroupDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改规格组
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        specGroupService.updatepecGroup(specGroupDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 新增规格参数
     */
    @PostMapping("/param")
    public ResponseEntity<Void> addSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        specParamService.addSpecParam(specParamDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据条件查询规格参数集合
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParam(
            @RequestParam(value = "categoryId", required = false)Long categoryId,
            @RequestParam(value = "groupId", required = false)Long groupId,
            @RequestParam(value = "searching", required = false)Boolean searching) {
        List<SpecParamDTO> specParamDTOS = specParamService.querySpecParam(categoryId, groupId, searching);
        return ResponseEntity.ok().body(specParamDTOS);
    }

    /**
     * 修改规格参数
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        specParamService.updateSpecParam(specParamDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除规格参数
     */
    @DeleteMapping("/param/{id}")
    public ResponseEntity<Void> deleteSpecParam(@PathVariable("id")Long pid) {
        specParamService.deleteSpecParam(pid);
        return ResponseEntity.ok().build();
    }

}
